package com.example.labtest.webapi;

import com.example.labtest.core.PageUtils;
import com.example.labtest.domain.Student;
import com.example.labtest.helper.R;
import com.example.labtest.helper.RUtil;
import com.example.labtest.service.AdminService;
import com.example.labtest.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 加一个restcontroller表明其是restcontroller
        */
@RestController
@RequestMapping("/webapi/admin")
public class AdminRestController {


    @Autowired
    private StudentService studentService;
    @Autowired
    private  AdminService adminService;

    /**
     * 读取的url：/webapi/admin/adlist
     * @return
     */
    @GetMapping("/adlist")
    public List<Student> getAll()
    {
        List<Student>students=studentService.findAll();
        return students;

    }

    /**
     * 分页,读取全部内容
     * 用如下两条语句传参数
     * @RequestParam(value="page",defaultValue = "0")Integer page,
     *                                    @RequestParam(value="size",defaultValue = "10")Integer size
     *
     * @return
     */
    @GetMapping("/getbypage")
    public R<Student> getByPage(@RequestParam(value="page",defaultValue = "0")Integer page,
                                @RequestParam(value="size",defaultValue = "10")Integer size,
                                @RequestParam(value = "sname",defaultValue = "")String sname
    ){
        //按id排倒序
        Sort sort= Sort.by(Sort.Direction.DESC, "id");

        Page<Student>studentPage;
        if(StringUtils.isEmpty(sname))
        {
            Pageable pageable=PageRequest.of(page,size,sort);
            studentPage =studentService.findAll(pageable);
        }
        else{
            Student student=new Student();
            student.setSname(sname);

            Pageable pageable=(Pageable) PageRequest.of(0,10,sort);
            //模糊查询,使用ExampleMatcher来匹配名字模糊查询，全部模糊查询 %{name}%
            ExampleMatcher matcher=ExampleMatcher.matching()
                    .withMatcher("sname",ExampleMatcher.GenericPropertyMatchers.contains());

            Example<Student>example=Example.of(student,matcher);

            studentPage=studentService.findAll(example,pageable);
        }

        PageUtils pageUtils=new PageUtils(studentPage.getContent(),studentPage.getTotalElements());
        return RUtil.success(pageUtils);
    }

    @GetMapping("/getbySex")
    public PageUtils getBySex(@RequestParam(value="page",defaultValue = "0")Integer page,

                              @RequestParam(value="size",defaultValue = "10")Integer size,
                              @RequestParam(value = "sex",defaultValue = "")String sex
    ){
        //按id排倒序
        Sort sort= Sort.by(Sort.Direction.DESC, "id");


        Page<Student>studentPage;
        if(StringUtils.isEmpty(sex))
        {
            Pageable pageable=PageRequest.of(page,size,sort);
            studentPage =studentService.findAll(pageable);
        }
        else{
            Student student=new Student();
            student.setSex(sex);

            Pageable pageable=(Pageable) PageRequest.of(0,10,sort);
            //模糊查询,使用ExampleMatcher来匹配名字模糊查询，全部模糊查询 %{name}%
            ExampleMatcher matcher=ExampleMatcher.matching()
                    .withMatcher("sex",ExampleMatcher.GenericPropertyMatchers.contains());

            Example<Student>example=Example.of(student,matcher);

            studentPage=studentService.findAll(example,pageable);
        }

        PageUtils pageUtils=new PageUtils(studentPage.getContent(),studentPage.getTotalElements());

        return pageUtils;

    }


    /**
     * 新增学生的方法./webapi/admin/insert(对应的url)
     * @param student
     * @return
     */
    //新增是一个post操作，所以需要postMapping
    @PostMapping("/insert")
    public Student insert(Student student){

        Student student1= studentService.insert(student);

        return student1;
    }

    /**
     * 读取的url：/webapi/admin/get/id
     * @return
     */
    @GetMapping("/get/{id}")
    public Student get(@PathVariable Long id)
    {
        Student student=studentService.getById(id);
        student.setPsw("");
        return student;

    }

    /**
     * 读取的url：/webapi/admin/findByName
     * @return
     */

    @GetMapping("/findByName")
    public R<Student> findByName(@RequestParam(value="page",defaultValue = "0")Integer page,
                                @RequestParam(value="size",defaultValue = "10")Integer size,
                                @RequestParam(value = "sname",defaultValue = "")String sname)
    {
        //按id排倒序
        Sort sort= Sort.by(Sort.Direction.DESC, "id");
        Page<Student>studentPage;
//        Page<Student>all;
//        Pageable pageable=PageRequest.of(offset/limit,limit,Sort.by("id").descending());
        if(sname == null||sname.equals("")){
            Pageable pageable=PageRequest.of(page,size,sort);
            studentPage=studentService.findAll(pageable);
        }
        else{
            Pageable pageable=PageRequest.of(page,size,sort);
            studentPage=studentService.findByNameLike("%"+sname+"%",pageable);
        }
        // List<Student> students = studentService.findByName(name);
        PageUtils pageUtils=new PageUtils(studentPage.getContent(),studentPage.getTotalElements());
        return RUtil.success(pageUtils);
    }


    /**
     * 更新学生的方法./webapi/admin/update(对应的url)
     * @param student
     * @return
     */
    //新增是一个put操作，所以需要postMapping
    @PutMapping("/update")
    public Student update(Student student){


        Student oldstudent=studentService.getById(student.getId());
        if(StringUtils.isEmpty(student.getPsw())){
            //没有新密码，那么就用旧密码
            student.setPsw(oldstudent.getPsw());
        }
        
        student=studentService.update(student);

        return student;
    }

    /**
     * 删除学生的方法./webapi/admin/delete(对应的url)
     * @param id
     */
    //修改成用路径的方式来删除
    @DeleteMapping("/delete/{id}")
    public void  delete(@PathVariable Long id){
        studentService.delete(id);
    }








}
